package com.example.zbh.dsliao.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.model.entity.NetSongList;
import com.example.zbh.dsliao.model.entity.SearchSingerDetails;

import java.util.List;

/**
 * Created by yaolin on 15/11/30.
 */
public class SearchSingerAdapter extends RecyclerView.Adapter<SearchSingerAdapter.MyViewHolder>{
    private List<SearchSingerDetails> datas;
    private setOnClickListener listener;


    /**监听事件**/
    public void setListener(setOnClickListener listener){
        this.listener = listener;
    }



    //行点击接口
     public interface setOnClickListener{
        void onItemClick(SearchSingerDetails datas, int position);
    }

    //下载歌曲接口
    public interface  OnDownLoaderClickListener{
        void OnDownLoaderClick(SearchSingerDetails searchSingerDetails);
    }
    private OnDownLoaderClickListener loaderClickListener;
    public void setOnDownLoaderClickListener(OnDownLoaderClickListener loaderClickListener){
        this.loaderClickListener = loaderClickListener;
    }

    /**添加数据**/
    public void addData(List<SearchSingerDetails> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.list_song, null);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if(datas != null && datas.size() > 0){
            SearchSingerDetails s = datas.get(position);
            holder.music_title.setText(s.getName());
            holder.music_artist.setText(s.getSingerName());
            holder.music_position.setText(position + 1 + " ");
        }
        holder.position = position;
    }

    @Override
    public int getItemCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }


    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView music_position, music_title, music_artist;
        private ImageView downLoad_img;
        private LinearLayout linearLayout;

        private int position;

        public MyViewHolder(View itemView) {
            super(itemView);
            music_position = (TextView) itemView.findViewById(R.id.music_position);
            music_title = (TextView) itemView.findViewById(R.id.music_title);
            music_artist = (TextView) itemView.findViewById(R.id.music_artist);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.linearLayout);
            linearLayout.setOnClickListener(this);
            downLoad_img = (ImageView) itemView.findViewById(R.id.downLoad_img);
            downLoad_img.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.linearLayout:
                    if (datas != null && datas.size() > 0) {
                        SearchSingerDetails s = datas.get(position);
                        if (listener != null) {
                            listener.onItemClick(s, position);
                        }
                    }
                    break;
                case R.id.downLoad_img:
                    if (loaderClickListener != null) {
                        SearchSingerDetails s = datas.get(position);
                        if (s != null) {
                            loaderClickListener.OnDownLoaderClick(s);
                        }
                    }
                    break;


            }

        }
    }
}
